// 简单实现 只能查找单个字符
// String.prototype.myIndexOf = function(searchItem, fromIndex) {
//   let len = this.length;
//   if (!fromIndex || fromIndex < 0) {
//     fromIndex = 0;
//   }
//   if (fromIndex >= len) {
//     fromIndex = len;
//   }
//   if (!searchItem) {
//     return fromIndex;
//   }
//   for (let i = fromIndex; i < len; i++) {
//     if (searchItem === this[i]) {
//       return i;
//     }
//   }
//   return -1;
// }

// 复杂版的  能查找字符串
String.prototype.myIndexOf = function(searchItem, fromIndex) {
  let len = this.length;
  if (!fromIndex || fromIndex < 0) {
    fromIndex = 0;
  }
  if (fromIndex >= len) {
    fromIndex = len;
  }
  if (!searchItem) {
    return fromIndex;
  }
  let j = 0;
  let index = -1;
  for (let i = fromIndex; i < len; i++) {
    if (this.charAt(i) === searchItem.charAt(0)) {
      let startIndex = i;
      let p = i;
      while (j < searchItem.length) {
        if (this.charAt(p) !== searchItem.charAt(j)) {
          j = 0;
          index = -1;
          break;
        }
        else {
          index = startIndex;
        }
        p++;
        j++;
      }
    }
  }
  return index;
}

let str = 'wzy';
console.log(str.myIndexOf('z', 3)); // -1
console.log(str.myIndexOf('z')); // 1
console.log(str.myIndexOf('z', -1)); // 1
console.log(str.myIndexOf('z', 10)); // -1
console.log(str.myIndexOf('')); // 0
console.log(str.myIndexOf('', 10)); // 3

console.log("Blue Whale".myIndexOf("Blue"));       // 返回 0
console.log("Blue Whale Blute".myIndexOf("Blute"));    // 返回 
console.log("Blue Whale".myIndexOf("Whale", 0));  // 返回 5
console.log("Blue Whale".myIndexOf("Whale", 5));   // 返回 5
console.log("Blue Whale".myIndexOf("", -1));       // 返回 0
console.log("Blue Whale".myIndexOf("", 9));        // 返回 9
console.log("Blue Whale".myIndexOf("", 10));       // 返回 10
console.log("Blue Whale".myIndexOf("", 11));       // 返回 10
console.log("Blue Whale".myIndexOf("blue"));         // 返回 -1
